package org.jsbd.boss.action;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.annotation.SystemControllerLog;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.domian.DaylimitBean;
import org.jsbd.boss.domian.channel.DaemonApkConfig;
import org.jsbd.boss.domian.channel.OS;
import org.jsbd.boss.service.ISystemService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.channel.trail.util.StringUtil;

/**
 * 
 * 系统管理
 * 
 * @author <a href="mailto:wangjunbiao@zhexinit.com" >王俊标</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("/system")
public class SystemController {

	public static final Logger LOGGER = LoggerFactory.getLogger(SystemController.class);

	@Resource
	private ISystemService systemService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list() {
		return "/system/list";
	}

	@RequestMapping(value = "listJson")
	@ResponseBody
	public Map<String, Object> listJson(OS os) {
		String version = os.getVersion();
		if (!StringUtils.isBlank(version)) {
			OS temp = systemService.getByVersion(version);
			if (null != temp && temp.getType() != null) {
				os.setType(temp.getType());
			} else {
				return new HashMap<String, Object>();
			}
		}
		return systemService.getSystemByPage(os);
	}

	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "/system/add";
	}

	@RequestMapping(value = "add", method = RequestMethod.POST)
	public void saveConfig(OS os, @RequestParam MultipartFile[] file, HttpServletResponse response) {
		try {
			systemService.saveSystem(file, os);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("保存系统信息出错", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "effect")
	@SystemControllerLog(description = "生效")
	public void effect(String mark, HttpServletResponse response) {
		try {
			systemService.updateSystem(mark, "0");
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("生效系统出错", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "abandon")
	@SystemControllerLog(description = "作废")
	public void abandon(String mark, HttpServletResponse response) {
		try {
			systemService.updateSystem(mark, "1");
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("废弃系统出错", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "/{id}/phoneTypeEdit", method = RequestMethod.GET)
	public String phoneTypeEdit(@PathVariable Integer id, Model model) {
		try {
			model.addAttribute("id", id);
		} catch (Exception e) {
			LOGGER.error(e.getMessage(), e);
		}
		return "system/phoneTypeEdit";
	}
	
	@RequestMapping(value = "/phoneTypeEdit", method = RequestMethod.POST)
	@ResponseBody
	public void phoneTypeEdit(OS os, HttpServletResponse response) {
		try {
			systemService.phoneTypeEdit(os);
	    	ResponseUtils.responseSuccess(response);
	    } catch (Exception e) {
	    	LOGGER.error(e.getMessage(), e);
	      ResponseUtils.responseFailure(response);
	    }
	}

}
